package com.ruoyi.eqmonitor.smdtv.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.eqmonitor.smdtv.domain.Smdtv10;
import com.ruoyi.eqmonitor.smdtv.domain.Smdtv11;
import com.ruoyi.eqmonitor.smdtv.mapper.Smdtv11Mapper;
import com.ruoyi.eqmonitor.smdtv.mapper.SmregisterMapper;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtv11Service;
import com.ruoyi.eqmonitor.smdtv.vo.SumVo;
import com.ruoyi.eqmonitor.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-09
 */
@Service
public class Smdtv11ServiceImpl extends ServiceImpl<Smdtv11Mapper,Smdtv11> implements ISmdtv11Service {

    @Resource
    private Smdtv11Mapper smdtv11Mapper;

    @Resource
    private SmregisterMapper smregisterMapper;

    @Override
    public Smdtv11 selectSmdtv11ById(Long id){
        return smdtv11Mapper.selectSmdtv11ById(id,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName()));
    }

    @Override
    public List<Smdtv11> selectSmdtv11List(Smdtv11 smdtv11,Integer pageNum,Integer pageSize){
        return smdtv11Mapper.selectSmdtv11List(smdtv11,
                smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName()),
                pageNum,
                pageSize);
    }

    @Override
    public Long getTotal(Smdtv11 smdtv11) {
        return smdtv11Mapper.getTotal(smdtv11,
                smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName()));
    }

    @Override
    public SumVo getSum(String name) {
        SumVo sumVo = new SumVo();
        Long value = smregisterMapper.getSum(name,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName()));
        sumVo.setName(Constant.Smregister.SMDTV_11.getName());
        sumVo.setValue(value);
        return sumVo;
    }

    @Override
    public List<Smdtv11> getByName(String name) {
        Smdtv11 smdtv11 = new Smdtv11();
        smdtv11.setName(name);
        return smdtv11Mapper.selectSmdtv11List(smdtv11,
                smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName()),
                null,null);
    }

    @Override
    public Map<String, List<String>> treeMap() {
        //最外层
        Map<String, List<String>> map = new HashMap<>();
        List<Smdtv11> list = smdtv11Mapper.selectSmdtv11List(new Smdtv11(),smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName()),
                null,null);
        //根据街道名称分组
        if (list != null) {
            Map<String, List<Smdtv11>> collect = list.stream().collect(Collectors.groupingBy(Smdtv11::getTown));
            for (String key : collect.keySet()) {
               //第二层
                List<String> stringList = new ArrayList<>();
                List<Smdtv11> smdtv11s2 = collect.get(key);
                if (smdtv11s2 != null){
                    for (Smdtv11 smdtv11 : smdtv11s2) {
                        stringList.add(smdtv11.getName());
                    }
                    map.put(key, stringList);
                }
            }

        }
        return map;
    }

    @Override
    public boolean save(Smdtv11 smdtv11) {
        return smdtv11Mapper.save(smdtv11,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName())) > 0;
    }

    @Override
    public boolean updateById(Smdtv11 smdtv11) {
        return smdtv11Mapper.updateById(smdtv11,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName())) > 0;
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        return smregisterMapper.removeByIds(ids,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_11.getName())) > 0;
    }

}
